package com.inews.business.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.security.vo.BaseUserVo;
import com.inews.base.utils.ExcelUtil;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.constants.InewsBusinessConstants;
import com.inews.business.entity.TblPostChange;
import com.inews.business.entity.TblTeacher;

import com.inews.business.entity.TblSysUser;


import com.inews.business.repository.IPostChangeDao;

import com.inews.business.vo.PostChangeVo;
import com.inews.business.vo.PostChangeVo;
import com.inews.business.vo.TrainInfoVo;

import com.inews.business.vo.TeacherVo;

@Service
public class PostChangeServiceImpl implements IPostChangeService, InewsBusinessConstants{

	
	@Autowired
	private IPostChangeDao postChangeDao;

	@Autowired
	private ITeacherService teacherService;
	
	@Autowired
	private BaseService baseService;
	
	@Override
	public ExtData<PostChangeVo> findPostChangePage(
			Map<String, Object> filterParams, Pageable page) {
		List<PostChangeVo> voList = new ArrayList<PostChangeVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		filters.put("status", new SearchFilter("status", Operator.EQ, POST_STATUS_NORMAL));
		Page<TblPostChange> pagePoList = postChangeDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblPostChange.class), page);
		for (TblPostChange po : pagePoList.getContent()) {
			PostChangeVo vo = new PostChangeVo();
			
			//添加人员信息
			teacherService.setTeacherInfo(vo, po.getStaffId());
			BeanMapper.copy(po, vo);
			
//			vo.setRetireDateFormat(InewsUtils.fomatDate(po.getRetireDate()));
//			vo.setCountryPayFormat(InewsUtils.formatCurrency2String(po.getCountryPay()));
//			vo.setCountryPayAddFormat(InewsUtils.formatCurrency2String(po.getCountryPayAdd()));
//			vo.setAllowanceFormat(InewsUtils.formatCurrency2String(po.getAllowance()));
			//Long basePay = po.getCountryPay() + po.getCountryPayAdd();
			//vo.setBasePayFormat(InewsUtils.formatCurrency2String(basePay));
			//Long sumPay = po.getCountryPay() + po.getCountryPayAdd() + po.getAllowance();
			//vo.setSumPayFormat(InewsUtils.formatCurrency2String(sumPay));
			voList.add(vo);
		}
		ExtData<PostChangeVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<PostChangeVo> savePostChange(PostChangeVo vo, BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblPostChange po = BeanMapper.map(vo, TblPostChange.class);
		po.setStatus(POST_STATUS_NORMAL);		

		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_ADD);
		po.setLastUpdTime(currentTime);
		po.setCreateTime(currentTime);
		po.setCreateUser(editUser);
		postChangeDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<PostChangeVo> updatePostChange(PostChangeVo vo,
			BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
		TblSysUser editUser = baseService.getEditUser(user);
		TblPostChange po = postChangeDao.findOne(vo.getSn());	
		BeanMapper.copy(vo,po);		
		po.setLastUpdUser(editUser);
		po.setStatus(POST_STATUS_NORMAL);		

		po.setLastUpdDes(LAST_UPD_DES_UPDATE);
		po.setLastUpdTime(currentTime);
		postChangeDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}

	@Override
	@Transactional(readOnly=false)
	public ExtData<PostChangeVo> deletePostChange(PostChangeVo vo,
			BaseUserVo user) {
		String currentTime = baseService.getCurrentTime();
	TblSysUser editUser = baseService.getEditUser(user);
		TblPostChange po = postChangeDao.findOne(vo.getSn());	
		po.setStatus(POST_STATUS_DELETE);
		po.setLastUpdUser(editUser);
		po.setLastUpdDes(LAST_UPD_DES_DELETE);
		po.setLastUpdTime(currentTime);
		postChangeDao.save(po);
		return ExtDataUtil.genWithSingleData(vo);
	}
	
	@Override
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<PostChangeVo> exportExcel(Map<String, Object> filterParams){
		List<PostChangeVo> allPostChangeVoList = new ArrayList<PostChangeVo>();
		
		
		
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		
		filters.put("status", new SearchFilter("status", Operator.EQ, InewsBusinessConstants.POST_STATUS_NORMAL));
		List<TblPostChange> result=	postChangeDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblPostChange.class));
				for (TblPostChange po : result) {
					PostChangeVo svo = BeanMapper.map(po, PostChangeVo.class);

					allPostChangeVoList.add(svo);
				}
					
		return ExtDataUtil.genWithData(allPostChangeVoList);
	}
	
	@Override
	/**
	 * 导入excel.
	 *
	 * @param excelFile the excel file
	 * @param user the user
	 * @return the ext data
	 */
	@Transactional(readOnly=false)
	public ExtData<String> importExcel(InputStream is, BaseUserVo user){
		try {
			List<Object> voList =ExcelUtil.ImportVo(is, ExcelUtil.PostChangeValue, PostChangeVo.class);
			 for (Object obj : voList) {
				 PostChangeVo vo=(PostChangeVo)obj;
			    	if(vo.getSn()==null||this.postChangeDao.exists(vo.getSn())){
			    		this.updatePostChange(vo, user);
					}else{
						this.savePostChange(vo, user);
					}
				}
				return ExtDataUtil.genWithSingleData("导入成功");
		} catch (Exception e) {		   
			throw new BusinessException("导入失败 失败的原因是"+e.getMessage());
		}
	}
}
